$(document).ready(function(){
	s=new slider3("#Gallery");
	
	/* Binding 'click' on Top button */
	$('a.top').bind('click', function(){
		$(document.body).scrollTo($('body'),600);
	})
	
	/* Binding 'click' on Bottom button */
	$('a.bottom').bind('click', function(){
		$(document.body).scrollTo($('a[name=video]'),600);
	})
	
	/* Binding 'click' on arrow buttons */
	/*$('.arrow').bind('mouseup', function(){
		evaluateTopBottomButtons();
	})*/
	
	var imgURL = "";
	if($('.activePage .Image').size() > 0){
		imgURL = $('.activePage .Image').css('background-image').replace('url(','').replace(')','');
		imgURL = imgURL.substr(imgURL.indexOf('http')).replace('"','');
	}
	
	if(document.location.href.indexOf("page=Home") > -1){
		var img = new Image();
		// wrap our new image in jQuery, then:
		$(img).load(function () {
	    	// Handling elements order display
	    	$('#slider').fadeIn(1000, function(){
	    		$('.anythingControls').slideDown('slow');
	    		$('.Artistes').fadeIn('slow');
	    		$('.Top').slideDown('slow');
	    		$('div.anythingSlider .back, div.anythingSlider .forward').fadeIn('500');
	    		evaluateTopBottomButtons();
	    	});
	    }).attr('src', imgURL);
	}else{
		$('#slider').fadeIn(1500);
		$('.anythingControls').slideDown('slow').parents('body').find('.Annee').slideDown('slow');
		$('.Artistes').fadeIn('slow');
		$('.Top').show();
		$('div.anythingSlider .back, div.anythingSlider .forward').fadeIn('500');
		evaluateTopBottomButtons();
	}
});

evaluateTopBottomButtons = function(){
	if( 	((document.location.href.indexOf("page=Home") > -1) && (($('#slider .activePage li .ContentPhotos').size() > 0) || ($('#slider .activePage li .Video').size() > 0)))
		||   (document.location.href.indexOf("page=Home") == -1) ){
		$('div.anythingSlider .back, div.anythingSlider .forward').fadeIn('500');
		$('.ScrollVertical:hidden').css('visibility','visible').fadeIn('500');
		$('.ScrollVerticalBG').fadeIn('500');
	}else{
		$('.ScrollVertical').fadeOut();
		$('.ScrollVerticalBG').fadeOut();
	}
}

var slider3 = function(id){
	var self=this;
	this.div = $(id);
	this.slider3= this.div.find(".slider3");
	this.largeurCache= this.div.width();
	this.largeur=0;
	this.div.find('li').each(function(){
		self.largeur+=$(this).width();
		self.largeur+=parseInt($(this).css("padding-left"));
		self.largeur+=parseInt($(this).css("padding-right"));
		self.largeur+=parseInt($(this).css("margin-left"));
		self.largeur+=parseInt($(this).css("margin-right"));
		})
	
	this.prev= this.div.find(".prev");
	this.next= this.div.find(".next");

	this.saut= this.largeurCache/2;
	this.nbEtapes= Math.ceil(this.largeur/this.saut - this.largeurCache/this.saut);
	this.courant=0;


	
	this.next.click(function(){
	if(self.courant<=self.nbEtapes){
		self.courant++;
		self.slider3.animate({
			left:(-self.courant*self.saut)
			},1000);
		}
	
	});

	this.prev.click(function(){
	if(self.courant>0){
		self.courant--;
		self.slider3.animate({
			left:-self.courant*self.saut
			},1000);
		}
	});
	

	$(function () {
		/*for (i=1; i<=self.nbEtapes; i++){ 
			var positionTimeline = $('.anythingSlider .next').click();
		}
		$('#slider').anythingSlider({
			  onInitialized       : positionTimeline
		});*/
		
		var positionToSetCurrentElement = 0; 
		
		if(($('body').width() - 266 < $('.slider3 li').size() * 125) && ($('.slider3 li a.cur').position().left + 60 > ($('body').width()/2))){
			positionToSetCurrentElement = $('.slider3 li a.cur').position().left - ($('body').width()/2) + 60;
			self.slider3.animate({
				left:(-1 * positionToSetCurrentElement)
			},1000);
			self.courant++;
		}
	});
}

